#!/usr/bin/env python
"""
PyLink-SR (Python Linked Software Radio System)
(c) Bob Cowdery (G3UKB) 2008

File: pylink_main.py
Date: July 2008

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:
The configuration module simply provides an abstraction over the config
file.
ToDo: Should services respond to messages?

"""

#-----------------------------------------------------------------------
# System imports
import sys
import os
import ConfigParser

# Application imports

#-----------------------------------------------------------------------
# The configuration file is a simple name/value file held in options.conf.
# This module simply provides a convienient interface to the values in 
# that file and also converts the values if required and provides defaults.
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
def open_config():
    global config
    config = ConfigParser.ConfigParser()
    # ToDo: find this file on the python path
    HOME = os.environ['PYLINK_HOME']
    path = HOME + 'conf/options.conf'
    #set = config.read(['../../conf/options.conf'])
    set = config.read([path])
    if len(set) == 0:
        print "Failed to open options.conf!!"
        print "The system cannot continue without this file..."
        print "PyLink-SR Closing..."
        sys.exit(1)

#-----------------------------------------------------------------------
# Modes
#-----------------------------------------------------------------------
def quiet():
    global config
    if config.has_option('Runtime', 'Quiet'):
        return config.getboolean('Runtime', 'Quiet')
    else:
        return False

#-----------------------------------------------------------------------
# Profile
#-----------------------------------------------------------------------
def ui_path():
    global config
    if config.has_option('Profile', 'UiPath'):
        return config.get('Profile', 'UiPath')
    else:
        return None 

#-----------------------------------------------------------------------
def dsp_path():
    global config
    if config.has_option('Profile', 'DspPath'):
        return config.get('Profile', 'DspPath')
    else:
        return None 

#-----------------------------------------------------------------------
def controller_path():
    global config
    if config.has_option('Profile', 'ControllerPath'):
        return config.get('Profile', 'ControllerPath')
    else:
        return None
    
#-----------------------------------------------------------------------
# Paths
#-----------------------------------------------------------------------
def log_file():
    global config
    if config.has_option('Paths', 'LogFile'):
        return config.get('Paths', 'LogFile')
    else:
        return None
    
#-----------------------------------------------------------------------
def glade_file():
    global config
    if config.has_option('Paths', 'GladeFile'):
        return config.get('Paths', 'GladeFile')
    else:
        return None 

#-----------------------------------------------------------------------
def server_path():
    global config
    if config.has_option('Paths', 'ServerPath'):
        return config.get('Paths', 'ServerPath')
    else:
        return ''
    
#-----------------------------------------------------------------------
def python_path():
    global config
    if config.has_option('Paths', 'PYTHONPATH'):
        return config.get('Paths', 'PYTHONPATH')
    else:
        return '' 
      
#-----------------------------------------------------------------------
# Pyro
#-----------------------------------------------------------------------
def pyro_local_host():
    global config
    if config.has_option('Pyro', 'LocalHost'):
        return config.get('Pyro', 'LocalHost')
    else:
        return None

#----------------------------------------------------------------------- 
def pyro_remote_host():
    global config
    if config.has_option('Pyro', 'RemoteHost'):
        return config.get('Pyro', 'RemoteHost')
    else:
        return None
    
#-----------------------------------------------------------------------
# Jack Audio
#-----------------------------------------------------------------------
def start_jack():
    global config
    if config.has_option('Jack', 'StartJack'):
        if config.get('Jack', 'StartJack').lower() == 'true':
            return True
        else:
            return False
    else:
        return False 

#-----------------------------------------------------------------------
def mode():
    global config
    if config.has_option('Jack', 'Mode'):
        return config.get('Jack', 'Mode')
    else:
        return 'standalone'
    
#-----------------------------------------------------------------------
def master_backend():
    global config
    if config.has_option('Jack', 'MasterBackend'):
        return config.get('Jack', 'MasterBackend')
    else:
        return 'alsa' 
 
#-----------------------------------------------------------------------
def slave_backend():
    global config
    if config.has_option('Jack', 'SlaveBackend'):
        return config.get('Jack', 'SlaveBackend')
    else:
        return 'net'
    
#-----------------------------------------------------------------------
def master_device():
    global config
    if config.has_option('Jack', 'MasterDevice'):
        return config.get('Jack', 'MasterDevice')
    else:
        return 'hw:0' 
    
#-----------------------------------------------------------------------
def smpl_rate():
    global config
    if config.has_option('Jack', 'Rate'):
        return int(config.get('Jack', 'Rate'))
    else:
        return 48000 

#-----------------------------------------------------------------------
def frames():
    global config
    if config.has_option('Jack', 'Frames'):
        return int(config.get('Jack', 'Frames'))
    else:
        return 1024 

#-----------------------------------------------------------------------
def periods():
    global config
    if config.has_option('Jack', 'Periods'):
        return int(config.get('Jack', 'Periods'))
    else:
        return 2 

#-----------------------------------------------------------------------
def softmode():
    global config
    if config.has_option('Jack', 'Softmode'):
        if config.get('Jack', 'Softmode').lower() == 'true':
            return True
        else:
            return False
    else:
        return False 

#-----------------------------------------------------------------------
def indev_ch1():
    global config
    if config.has_option('Jack', 'InDevCh1'):
        return config.get('Jack', 'InDevCh1')
    else:
        return 'alsa_pcm:capture_1' 

#-----------------------------------------------------------------------
def indev_ch2():
    global config
    if config.has_option('Jack', 'InDevCh2'):
        return config.get('Jack', 'InDevCh2')
    else:
        return 'alsa_pcm:capture_2'

#-----------------------------------------------------------------------
def outdev_ch1():
    global config
    if config.has_option('Jack', 'OutDevCh1'):
        return config.get('Jack', 'OutDevCh1')
    else:
        return 'alsa_pcm:playback_1' 

#-----------------------------------------------------------------------
def outdev_ch2():
    global config
    if config.has_option('Jack', 'OutDevCh2'):
        return config.get('Jack', 'OutDevCh2')
    else:
        return 'alsa_pcm:playback_2'
 
#-----------------------------------------------------------------------
def inslave_ch1():
    global config
    if config.has_option('Jack', 'InSlaveCh1'):
        return config.get('Jack', 'InSlaveCh1')
    else:
        return 'system:capture_1' 

#-----------------------------------------------------------------------
def inslave_ch2():
    global config
    if config.has_option('Jack', 'InSlaveCh2'):
        return config.get('Jack', 'InSlaveCh2')
    else:
        return 'system:capture_2'

#-----------------------------------------------------------------------
def outslave_ch1():
    global config
    if config.has_option('Jack', 'OutSlaveCh1'):
        return config.get('Jack', 'OutSlaveCh1')
    else:
        return 'system:playback_1' 

#-----------------------------------------------------------------------
def outslave_ch2():
    global config
    if config.has_option('Jack', 'OutSlaveCh2'):
        return config.get('Jack', 'OutSlaveCh2')
    else:
        return 'system:playback_2'
    
#-----------------------------------------------------------------------
def innet_ch1():
    global config
    if config.has_option('Jack', 'InNetCh1'):
        return config.get('Jack', 'InNetCh1')
    else:
        return 'net_source:capture_1' 

#-----------------------------------------------------------------------
def innet_ch2():
    global config
    if config.has_option('Jack', 'InNetCh2'):
        return config.get('Jack', 'InNetCh2')
    else:
        return 'net_source:capture_2'

#-----------------------------------------------------------------------
def outnet_ch1():
    global config
    if config.has_option('Jack', 'OutNetCh1'):
        return config.get('Jack', 'OutNetCh1')
    else:
        return 'net_source:playback_1' 

#-----------------------------------------------------------------------
def outnet_ch2():
    global config
    if config.has_option('Jack', 'OutNetCh2'):
        return config.get('Jack', 'OutNetCh2')
    else:
        return 'net_source:playback_2'
    
#-----------------------------------------------------------------------
def inalsa_ch1():
    global config
    if config.has_option('Jack', 'InAlsaCh1'):
        return config.get('Jack', 'InAlsaCh1')
    else:
        return 'alsa_in:capture_1' 

#-----------------------------------------------------------------------
def inalsa_ch2():
    global config
    if config.has_option('Jack', 'InAlsaCh2'):
        return config.get('Jack', 'InAlsaCh2')
    else:
        return 'alsa_in:capture_2'

#-----------------------------------------------------------------------
def outalsa_ch1():
    global config
    if config.has_option('Jack', 'OutAlsaCh1'):
        return config.get('Jack', 'OutAlsaCh1')
    else:
        return 'alsa_out:playback_1' 

#-----------------------------------------------------------------------
def outalsa_ch2():
    global config
    if config.has_option('Jack', 'OutAlsaCh2'):
        return config.get('Jack', 'OutAlsaCh2')
    else:
        return 'alsa_out:playback_2'
    
#-----------------------------------------------------------------------
def slave_host():
    global config
    if config.has_option('Jack', 'SlaveHost'):
        return config.get('Jack', 'SlaveHost')
    else:
        return None

#-----------------------------------------------------------------------
def slave_device():
    global config
    if config.has_option('Jack', 'SlaveDevice'):
        return config.get('Jack', 'SlaveDevice')
    else:
        return 'hw:0'
    
#-----------------------------------------------------------------------
def catch_factor():
    global config
    if config.has_option('Jack', 'CatchFactor'):
        return int(config.get('Jack', 'CatchFactor'))
    else:
        return 30000
    
#-----------------------------------------------------------------------
def max_diff():
    global config
    if config.has_option('Jack', 'MaxDiff'):
        return int(config.get('Jack', 'MaxDiff'))
    else:
        return 64
    
#-----------------------------------------------------------------------
# DSP
#-----------------------------------------------------------------------
def dsp_agc_ramp():
    global config
    if config.has_option('DSP', 'AgcRamp'):
        return float(config.get('DSP', 'AgcRamp'))
        
#-----------------------------------------------------------------------
def dsp_agc_gain():
    global config
    if config.has_option('DSP', 'AgcGain'):
        return float(config.get('DSP', 'AgcGain'))

#-----------------------------------------------------------------------
def dsp_agc_roof():
    global config
    if config.has_option('DSP', 'AgcRoof'):
        return float(config.get('DSP', 'AgcRoof'))
    
#-----------------------------------------------------------------------
# SDR1000
#-----------------------------------------------------------------------
def sdr1000_rfe():
    global config
    if config.has_option('SDR1000', 'RFE'):
        if (config.get('SDR1000', 'RFE')).lower() == 'true':
            return True
        else:
            return False
    else:
        return False

#-----------------------------------------------------------------------
def sdr1000_pa():
    global config
    if config.has_option('SDR1000', 'PA'):
        if (config.get('SDR1000', 'PA')).lower() == 'true':
            return True
        else:
            return False
    else:
        return False

#-----------------------------------------------------------------------
def sdr1000_usb():
    global config
    if config.has_option('SDR1000', 'USB'):
        if (config.get('SDR1000', 'USB')).lower() == 'true':
            return True
        else:
            return False
    else:
        return False

#-----------------------------------------------------------------------
def sdr1000_addr():
    global config
    if config.has_option('SDR1000', 'Addr'):
        return int(config.get('SDR1000', 'Addr'))
    else:
        return 0x378
    
#-----------------------------------------------------------------------
# Error Codes

         
